/*
 * ImportSources module for Netbeans.
 *
 * Copyright (c) 2007, Rogue
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * For queries or bugs, you may contact me via e-mail: roguexz [at] gmail [dot] com
 */
package rogue.netbeans.module.importsources.ui;

import java.awt.Component;
import java.awt.Point;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.DocumentListener;
import org.openide.windows.WindowManager;

/**
 * UI representation of the WizardPanel1.
 *
 * @author Rogue
 */
public final class WizardPanel1UI extends JPanel {
    
    /**
     * Creates new form WizardPanel1UI
     */
    public WizardPanel1UI(DocumentListener lsnr) {
        initComponents();
        
        // Initialize the file chooser
        fileChooser = new JFileChooser();
        
        File f = new File(getSelectedDirectory());
        if(f.isDirectory()) {
            fileChooser.setCurrentDirectory(f);
        }
        
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        fileChooser.setDialogTitle("Specify the directory from where the sources need to be imported.");
        fileChooser.setMultiSelectionEnabled(false);
        
        rootDirectoryTF.getDocument().addDocumentListener(lsnr);
    }
    
    public String getName() {
        return "Select a directory.";
    }
    
    public String getSelectedDirectory() {
        return rootDirectoryTF.getText();
    }
    
    public String[] getFileExensions() {
        if(selectAllFileRB.isSelected()) {
            return new String[]{""};
        } else {
            String[] ext = fileExtensionsTF.getText().split(",");
            List<String> extList = new ArrayList<String>();
            for(int i=0; i<ext.length; i++) {
                ext[i] = ext[i].trim();
                if(ext[i].length()!=0) {
                    extList.add(ext[i]);
                }
            }
            return extList.toArray(new String[extList.size()]);
        }
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        rootDirectoryTF = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        selectAllFileRB = new javax.swing.JRadioButton();
        customFilesRB = new javax.swing.JRadioButton();
        fileExtensionsTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Specify the directory from which the sources need to be imported.");
        jLabel1.setFocusable(false);

        jButton1.setMnemonic('w');
        org.openide.awt.Mnemonics.setLocalizedText(jButton1, "Browse");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "Specify the file extensions to be matched.");

        buttonGroup1.add(selectAllFileRB);
        selectAllFileRB.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        selectAllFileRB.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup1.add(customFilesRB);
        customFilesRB.setSelected(true);
        customFilesRB.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        customFilesRB.setMargin(new java.awt.Insets(0, 0, 0, 0));
        customFilesRB.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                customFilesRBStateChanged(evt);
            }
        });

        fileExtensionsTF.setText(".java");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "All files");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, "<html>\n<u>Hint:</u> Specify the extensions as a comma separated list.<br/>\ne.g.: .java, .xml, .jsp\n</html>");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(rootDirectoryTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1))
                    .add(layout.createSequentialGroup()
                        .add(4, 4, 4)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(customFilesRB)
                            .add(selectAllFileRB))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(fileExtensionsTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(4, 4, 4)
                                .add(jLabel4)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(rootDirectoryTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .add(7, 7, 7)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(selectAllFileRB)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(customFilesRB)
                    .add(fileExtensionsTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel4)
                .addContainerGap(145, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void customFilesRBStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_customFilesRBStateChanged
        fileExtensionsTF.setEnabled(((JRadioButton)evt.getSource()).isSelected());
    }//GEN-LAST:event_customFilesRBStateChanged
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        // center the file chooser w.r.t the top component.
        Component parent = WindowManager.getDefault().getMainWindow();
        Point parentLocation = parent.getLocationOnScreen();
        int x = parentLocation.x + parent.getWidth()/2;
        int y = parentLocation.y + parent.getHeight()/2;
        
        fileChooser.setLocation(x,y);
        
        if(fileChooser.showOpenDialog(parent)==JFileChooser.APPROVE_OPTION) {
            rootDirectoryTF.setText(fileChooser.getSelectedFile().toString());
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton customFilesRB;
    private javax.swing.JTextField fileExtensionsTF;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField rootDirectoryTF;
    private javax.swing.JRadioButton selectAllFileRB;
    // End of variables declaration//GEN-END:variables
    private JFileChooser fileChooser = null;
}

